package appnode;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;
import javax.servlet.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import appnode.Node;
import appnode.Linktools;
import appnode.SearchNode;
import appnode.PMF;

public class Mapservlet extends HttpServlet {
	
		
	 public void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
			
		
			HttpSession session = req.getSession(false); 
			Vector mapvector = (Vector) session.getValue("map");
			
			// user
		    UserService userService = UserServiceFactory.getUserService();
		    User user = userService.getCurrentUser();
		
			PersistenceManager pm = PMF.get().getPersistenceManager();	
			String action = req.getParameter("showmap");
			String nodename = req.getParameter("nodename");
			
			mapvector = new Vector();
			mapvector.add(nodename);
			session.putValue("map",mapvector);
				
				// show map
				if (user != null) {
					// add node
					if (nodename.equals("add")) {
							String url = "/addnode.jsp";
							ServletContext sc = getServletContext();
							RequestDispatcher rd = sc.getRequestDispatcher(url);			
							rd.forward(req, resp);
					}
					else { 
											
						Node init_node = SearchNode.getByName(nodename);
				
						if ((SearchNode.validatePage(init_node.getNodename(),user.getNickname()).equals("yes"))) {
							req.setAttribute("nodename",nodename);
							String url = "/maps.jsp";
							ServletContext sc = getServletContext();
							RequestDispatcher rd = sc.getRequestDispatcher(url);			
							rd.forward(req, resp);
						} // show map
						else {
							resp.setContentType("text/html");
							resp.getWriter().println("<html>");
							resp.getWriter().println("permission denied");
							resp.getWriter().println("</html>");	
						}
					}					
					
				}
				else {
					String url = "/";
					ServletContext sc = getServletContext();
					RequestDispatcher rd = sc.getRequestDispatcher(url);			
					rd.forward(req, resp);
				}
				
				
				
				
		} // httpservlet
		
} // Mapservlet